package edu.sctu.jsoup.service;


import edu.sctu.jsoup.entity.AllBbsEntity;
import edu.sctu.jsoup.entity.Table3Entity;
import edu.sctu.jsoup.repo.AllBbsRepo;
import edu.sctu.jsoup.repo.Table3Repo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;

@Service
public class Table3Service {

    @Autowired
    private AllBbsRepo allBbsRepo;

    @Autowired
    private Table3Repo table3Repo;


    public void run() throws IOException, InterruptedException {

        /*得到表二存入集合，序号从0开始*/
        List<AllBbsEntity> allBbsEntities = allBbsRepo.findAll();


        /*获取断点*/

        /*
        * 注意：
        * getTable3.txt文件同样需要自己在D盘dev下新建，并把初始值0写入
        * 之后出现断点会自动更新
        * */
        File filename = new File("D:\\dev\\getTable3.txt");

        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));

        BufferedReader br = new BufferedReader(reader);

        String line = br.readLine();

        int nextStart = Integer.parseInt(line);//将读取到的字符串转化为int类型


        /*for循环遍历表二，初始值为0，在txt文件设置*/
        for (int i = nextStart; i < allBbsEntities.size(); i++) {//表二总长为allBbsEntities.size()

            /*一秒钟请求一个网页*/
            Thread.currentThread().sleep(1000);


            /*每50次请求后暂停30秒后再继续请求*/
            if (i % 50 == 0) {
                System.out.println("===============休息30秒===============");
                Thread.currentThread().sleep(30000);
            }


            Table3Entity table3Entity = new Table3Entity();
            /*得到表二每条记录上的URL*/
            String url = allBbsEntities.get(i).getUrl();
            /*向服务器请求源码*/
            Document document = Jsoup.connect(url).get();

            /*设置相关信息并保存*/
            table3Entity.setHtmlContent(document.toString());
            table3Entity.setPage(allBbsEntities.get(i).getPage());
            table3Entity.setTitle(allBbsEntities.get(i).getTitle());
            table3Entity.setUrl(url);
            table3Repo.save(table3Entity);



            /*存储下次请求的开始值或断点*/
            File writename = new File("D:\\dev\\getTable3.txt");

            writename.createNewFile(); // 创建新文件

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));

            out.write(Integer.toString(i + 1));

            // 把缓存区内容压入文件
            out.flush();
            // 最后关闭文件
            out.close();
            System.out.println("已请求到第"+i+"条记录!" +
                    "\n下次请求的开始值"+(i+1)+"已记录！");

        }
    }
}

